package com.study.service;

import com.study.domain.SMSCode;
import com.study.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class SMSCodeServiceImpl implements SMSCodeService {

    @Autowired
    private CodeUtils codeUtils;

    @Override
    @CachePut(value = "smsCode",key="#tele")
    public String sendCodeToSMS(String tele) {
        String code = codeUtils.getCode(tele);
        System.out.println("生成的验证码是："+code+"!");
        return code;
    }

    @Override
    public boolean checkCode(SMSCode smsCode) {
        //取出内存中的验证码与传递过来的验证码比对，如果相同，返回true
        System.out.println("ServiceImpl checkCode开始校验smsCode=》"+smsCode);
        String code = smsCode.getCode();
        String cacheCode = codeUtils.get(smsCode.getTele());

        System.out.println("从缓存获取的cacheCode=>"+cacheCode);//null


        return code.equals(cacheCode);
    }
}